;; use monte carlo to get Pi
;; cesaro's method for estimate Pi
;;   Prob(gcd(a, b) == 1) = 6/(pi*pi)

(define tries-count 10000000)
(define random-limit 1000000)

(define (monte-carlo fn)
  (monte-iter tries-count 0 fn))

(define (monte-iter tries result fn)
  (cond
    [(= tries 0) result]
    [(fn) (monte-iter (-1+ tries) (1+ result) fn)]
    [else (monte-iter (-1+ tries) result fn)]
    ))

(define (gcd a b)
  (cond
    [(= (mod a b) 0) b]
    [else (gcd b (mod a b))]))

(define (random_range low high)
  (+ (random (- high low)) low))

(define (cesaro)
  (= (gcd (random_range 1 random-limit)
          (random_range 1 random-limit)) 1))

(define (cal-pi)
  (let ([prob (/ (monte-carlo cesaro) tries-count)])
    ;; (display prob)
    (sqrt (/ 6.0 prob))))
